﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using Microsoft.Windows.Controls.Ribbon;
using Microsoft.Windows.Controls;  

namespace ScrumMagic
{
    /// <summary>
    /// Interaction logic for TaskListForm.xaml
    /// </summary>
    /// пример использования общей формы. эту форму нужно наследовать от  ScrumMagic.CommonForm. 
    /// наследовать не только здесь, но и в xaml файле.
    public partial class TaskListForm : ScrumMagic.CommonForm  
    {
        private TaskListRibbon taskListRibbon;

        public TaskListForm()
        {
            InitializeComponent();

            taskListRibbon = new TaskListRibbon();
            taskListRibbon.FindButtonClicked += this.OnMenuFind;
            taskListRibbon.FilterButtonClicked += new EventHandler<TextEventArgs>(taskListRibbon_FilterButtonClicked);
        }

        void taskListRibbon_FilterButtonClicked(object sender, TextEventArgs e)
        {
            MessageBox.Show("Filtering!");
        }

        //TaskListRibbon LocalMenu
        //{
        //  get { return taskListRibbon; }
        //}

        #region overriden methods
        /// <summary>
        /// Returns contextual tab of this form.
        /// </summary>
        public override RibbonTab RibbonContextualTab
        {
            get
            {
                taskListRibbon.ribbonMenu.Tabs.Remove(taskListRibbon.tabEdit);
                return taskListRibbon.tabEdit;
            }
        }

        public override void OnActivated()
        {
          try
          {
            SqlCommand com = DBController.MainConnection.CreateCommand();
            com.CommandText = 
                "SELECT " +
                "t.Id, "+
                "t.Name, " +
                "t.Comment, " +
                "tp.Name AS TaskPriority, " +
                "u.Login AS [User], " +
                "t.Estimation, " +
                "t.Remain, " +
                "t.DayTime " +
                "FROM Task t INNER JOIN " +
                "TaskPriority tp ON tp.Id = t.TaskPriority_Id INNER JOIN " +
                "[User] u ON u.Id = t.User_Id ";
            com.CommandType = System.Data.CommandType.Text;
            SqlDataAdapter adapter = new SqlDataAdapter(com);
            DataTable tableWithId = new DataTable();
            adapter.Fill(tableWithId);
            DataTable table = tableWithId;
            tableWithId.Columns.Remove("id");
            dataGrid.ItemsSource = table.DefaultView;
              
          }
          catch (Exception ex)
          {
            MessageBox.Show("Can't get table Task",
              "ScrumMagic",
              MessageBoxButton.OK);
          }
        }

        #endregion

        #region Event handlers
        void OnDataGridDoubleClick(object sender, MouseButtonEventArgs e)
        {
          DataRowView selectedRow = (DataRowView)dataGrid.SelectedItem;

          if (selectedRow == null) return;

          // пример вызова карточки из списка
          CommonForm FormToOpen = new TaskCardForm();
          FormToOpen.FormData = selectedRow;
          FormToOpen.Title = selectedRow["Name"].ToString();
          FormController.OpenForm(FormToOpen, FormController.ContainerTypes.window); 

        }

        #endregion

        #region Find
        /// <summary>
        /// Test handler for search.
        /// </summary>
        public void OnMenuFind(object sender, TextEventArgs e)
        {
            DataView dataSourse = dataGrid.ItemsSource as DataView;
            dataSourse.RowFilter = string.Format("Name LIKE '{0}%'", e.Text);
        }
        #endregion

        private void CommonForm_Drop(object sender, DragEventArgs e)
        {
           
        }
    }
}
